//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    flashPicData:[],
    contentList:[],
    userId:''
  },
  //选择某一项
  chooseOne:function(e){
    console.log(e.currentTarget.dataset.no);
    wx.navigateTo({
      url: '../content/content?id='+e.currentTarget.dataset.no
    })
  },
    //点赞
  chooseLike:function(e){
    var that = this;
    console.log(e.currentTarget.dataset.no);
    wx.request({
      url:app.URL+'/edit_like_num',
      data:{
        id:e.currentTarget.dataset.no,
        userId:that.data.userId
      },
      success:function(data){
        console.log(data);
        that.fetchData();
      }
    })
  },
    //进入评论
  chooseMark:function(e){
    console.log(e.currentTarget.dataset.no);
    wx.navigateTo({
      url: '../mark/mark?id='+e.currentTarget.dataset.no
    })
  },
  //事件处理函数
  onLoad: function (options) {
    console.log('onLoad')
    var that = this;
    that.setData({
        _id:options.tag,
        options:options
    });
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      console.log(userInfo)
      //更新数据
    });
    wx.login({
      success: (res)=>{
        console.log(res.code);
        wx.request({
           url:'https://api.weixin.qq.com/sns/jscode2session?appid=wxf89f318b1e769832&secret=38f45a85816ef2f9508f9b2c58adfa81&js_code='+res.code+'&grant_type=authorization_code',
           success:function(data){
              console.log(data.data.openid)
              that.setData({
                  userId:data.data.openid
              });
             if(options.tag =='mark'){
                var Url = app.URL+'/get_mark_content?id='+that.data.userId
              }
              if(options.tag =='like'){
                var Url = app.URL+'/get_like_content?id='+that.data.userId
              }
              that.fetchData(Url);
           }
         })
      },
    });
  },
  //获取数据
    fetchData:function(Url){
    var that = this
    console.log(Url);
    wx.request({
      url: Url,
      success:function(data){
        if(data.statusCode==200){
            console.log(data.data);
            that.setData({
              flashPicData:data.data,
              contentList:data.data
            });
            var length = that.data.flashPicData.length
            var j=0;
            setInterval(function(){
                j++;
                if(j>=length){
                  j=0;
                }
            },3000);
        } else{
          console.log("error")
        }
      }
    });
  },
  //获取openid
  getOpenID:function(){
    var that = this;
    wx.login({
      success: (res)=>{
       // console.log(res.code);
        wx.request({
           url:'https://api.weixin.qq.com/sns/jscode2session?appid=wx43aa02486e6d3f7d&secret=7455a8c932c7b162a9134643379cdc47&js_code='+res.code+'&grant_type=authorization_code',
           success:function(data){
              //console.log(data.data.openid)
              that.setData({
                  userId:data.data.openid
              });
           }
         })
      },
    })
  }
})
